/*
 * BooTie.cpp
 *
 * Created: 24-02-2013 13:16:16
 *  Author: Bootie
 */ 
#include <avr/io.h>
#include "Bootloader/bootloader.h"
#include <stdlib.h>
#include "FatFS/ff.h"
#include "FatFS/diskio.h"
#include <avr/delay.h>

BOOTLOADER_SECTION main(void)
{
    // Setup
    // Disable all interrupts while boot loading
    //__asm ("cli"); // Interrupts being used by FatFS
    // Clear SREG
    __asm ("eor r1,r1");
    __asm	("out 0x3f,r1");
    // Stack pointer = 0x0800
    __asm ("ldi r28,0x5F");
    __asm	("ldi r29,0x08");
    __asm ("out 0x3e,r29");
    __asm ("out 0x3d,r28" );

    // Run bootloader
    if(Bootloader::GetInstance().ProgramAvailable())
    {
        Bootloader::GetInstance().LoadProgram();
    }
    Bootloader::GetInstance().RunProgram();
    while(1){} // Should not reach here!
}